#include "./sensor_manager.h"

const int GLOBAL_RESMGR = 0xFF00;
const char *TAG = "[Sensor]";
Sensor_SubscriptionId *id = nullptr;
Sensor_Subscriber *g_user = nullptr;
Sensor_SubscriptionAttribute *attr = nullptr;

napi_value SensorManager::GetSensorInfos(napi_env env, napi_callback_info info)
{
    uint32_t count = 0;
    int32_t ret = OH_Sensor_GetInfos(nullptr, &count); // 获取设备上所有传感器的个数。 
    if (ret != SENSOR_SUCCESS) {
        return nullptr;
    }
    napi_value sum;
    napi_create_int32(env, count, &sum);
    Sensor_Info **sensors = OH_Sensor_CreateInfos(count); // 用给定的数字创建一个实例数组。
    if (sensors == nullptr) {
        return nullptr;
    }        
    ret = OH_Sensor_GetInfos(sensors, &count); // 获取设备上所有传感器的信息。 
    if (ret != SENSOR_SUCCESS) {
        return nullptr;
    }
    OH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, TAG, "GetSensorInfos sucessful");
    ret = OH_Sensor_DestroyInfos(sensors, count); // 销毁实例数组并回收内存。
    if (ret != SENSOR_SUCCESS) {
        return nullptr;
    }
    return sum;
}


void SensorDataCb(Sensor_Event *event)
{
    if (event == nullptr) {
        OH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, TAG, "event is null");
        return;
    }
    int64_t timestamp = SensorManager::INVALID_VALUE;
    int32_t ret = OH_SensorEvent_GetTimestamp(event, &timestamp); // 获取传感器数据的时间戳。
    if (ret != SENSOR_SUCCESS) {
        return;
    }
    Sensor_Type sensorType;
    ret = OH_SensorEvent_GetType(event, &sensorType); // 获取传感器类型。
    if (ret != SENSOR_SUCCESS) {
        return;
    }
    Sensor_Accuracy accuracy = SENSOR_ACCURACY_UNRELIABLE;
    OH_SensorEvent_GetAccuracy(event, &accuracy); // 获取传感器数据的精度。
    float *data = nullptr;
    uint32_t length = 0;
    ret = OH_SensorEvent_GetData(event, &data, &length); // 获取传感器数据。
    if (ret != SENSOR_SUCCESS) {
        return;
    }
    for (uint32_t i = 0; i < length; ++i) {
        OH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, TAG, "data[%{public}d]:%{public}f", i, data[i]);
    }
}

napi_value SensorManager::Subscriber(napi_env env, napi_callback_info info)
{
    g_user = OH_Sensor_CreateSubscriber(); // 创建一个Sensor_Subscriber实例。
    int32_t ret = OH_SensorSubscriber_SetCallback(g_user, SensorDataCb); // 设置一个回调函数来报告传感器数据。
    if (ret != SENSOR_SUCCESS) {
        return nullptr;
    }
    id = OH_Sensor_CreateSubscriptionId(); // 创建一个Sensor_SubscriptionId实例。
    OH_SensorSubscriptionId_SetType(id, SENSOR_ID);
    attr = OH_Sensor_CreateSubscriptionAttribute(); // 创建Sensor_SubscriptionAttribute实例。
    OH_SensorSubscriptionAttribute_SetSamplingInterval(attr, SENSOR_SAMPLE_PERIOD); // 设置传感器数据报告间隔。
    ret = OH_Sensor_Subscribe(id, attr, g_user); // 订阅传感器数据。
    if (ret != SENSOR_SUCCESS) {
        return nullptr;
    }
    OH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, TAG, "SensorManager::Subscriber successful");
    napi_value sum;
    napi_create_int32(env, ret, &sum);
    return sum;
}

napi_value SensorManager::UnSubscriber(napi_env env, napi_callback_info info)
{
    if (attr != nullptr && g_user != nullptr && id != nullptr) {
        int32_t ret = OH_Sensor_Unsubscribe(id, g_user); // 取消订阅传感器数据。
        if (ret != SENSOR_SUCCESS) {
            return nullptr;
        }
        if (id != nullptr) {
            OH_Sensor_DestroySubscriptionId(id);
            id = nullptr;
        }
        if (attr != nullptr) {
            OH_Sensor_DestroySubscriptionAttribute(attr);
            attr = nullptr;
        }
        if (g_user != nullptr) {
            OH_Sensor_DestroySubscriber(g_user);
            g_user = nullptr;
        }
        OH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, TAG, "SensorManager::UnSubscriber successful");
        napi_value sum;
        napi_create_int32(env, ret, &sum);
        return sum;
    } else {
        return nullptr;
    }
}
